package com.scu.dao;

import com.scu.entity.Post;
import com.scu.entity.mPost;
import com.scu.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class mPostDao {
    //用来加入一条新的帖子,帖子的id交给数据库生成，所以添加的时候不需要写id，需要传入一个post对象
    public static int insert(mPost post) throws SQLException {
        //1. 连接
        Connection conn = JDBCUtil.getConnection();

        //2. sql
        String sql = "INSERT INTO post(post_title,post_content,user_id,type,is_illegal,dates) VALUES(?,?,?,?,?,?)";
        PreparedStatement prep = conn.prepareStatement(sql);
        //prep.setInt(1, post.getPost_id());

        prep.setString(1, post.getPost_title());
        prep.setString(2, post.getPost_content());
        prep.setInt(3, post.getUser_id());
        prep.setString(4, post.getType());
        prep.setInt(5, post.getIs_illegal());
        prep.setString(6, post.getDates());



        //3. 结果
        int row = prep.executeUpdate();

        //4. 关闭连接
        conn.close();

        return row;
    }

    //用来显示所有帖子，用在广场页面
    public List<mPost> findAll() throws SQLException {

        Connection conn = JDBCUtil.getConnection();

        String sql = "select * from post where type = 1 ";
        PreparedStatement prep = conn.prepareStatement(sql);

        ResultSet rs = prep.executeQuery();

        List<mPost> list = new ArrayList<mPost>();
        while(rs.next()){
            //rs代表的就是每一行
            mPost post = new mPost();

            int post_id = rs.getInt("post_id");
            String post_title = rs.getString("post_title");
            String post_content = rs.getString("post_content");
            int user_id = rs.getInt("user_id");
            String type = rs.getString("type");
            int is_illegal = rs.getInt("is_illegal");
            String dates = rs.getString("dates");


            post.setPost_id(post_id);
            post.setPost_title(post_title);
            post.setPost_content(post_content);
            post.setUser_id(user_id);
            post.setType(type);
            post.setIs_illegal(is_illegal);
            post.setDates(dates);

            list.add(post);
        }

        //4. 关闭连接
        conn.close();

        return list;
    }

    public List<mPost> findLife() throws SQLException {

        Connection conn = JDBCUtil.getConnection();

        String sql = "select * from post where type = 3 ";
        PreparedStatement prep = conn.prepareStatement(sql);

        ResultSet rs = prep.executeQuery();

        List<mPost> list = new ArrayList<mPost>();
        while(rs.next()){
            //rs代表的就是每一行
            mPost post = new mPost();

            int post_id = rs.getInt("post_id");
            String post_title = rs.getString("post_title");
            String post_content = rs.getString("post_content");
            int user_id = rs.getInt("user_id");
            String type = rs.getString("type");
            int is_illegal = rs.getInt("is_illegal");
            String dates = rs.getString("dates");


            post.setPost_id(post_id);
            post.setPost_title(post_title);
            post.setPost_content(post_content);
            post.setUser_id(user_id);
            post.setType(type);
            post.setIs_illegal(is_illegal);
            post.setDates(dates);

            list.add(post);
        }

        //4. 关闭连接
        conn.close();

        return list;
    }

    //在个人主页展示自己发过的帖子
    public List<mPost> findSpeUser(int user_id) throws SQLException {
        Connection conn = JDBCUtil.getConnection();

        String sql = "select * from post where user_id = ?";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setInt(1,user_id);

        ResultSet rs = prep.executeQuery();

        List<mPost> list = new ArrayList<mPost>();
        while(rs.next()){
            //rs代表的就是每一行
            mPost post = new mPost();

            int post_id = rs.getInt("post_id");
            String post_title = rs.getString("post_title");
            String post_content = rs.getString("post_content");
            int user_ids = rs.getInt("user_id");
            String type = rs.getString("type");
            int is_illegal = rs.getInt("is_illegal");
            String dates = rs.getString("dates");


            post.setPost_id(post_id);
            post.setPost_title(post_title);
            post.setPost_content(post_content);
            post.setUser_id(user_ids);
            post.setType(type);
            post.setIs_illegal(is_illegal);
            post.setDates(dates);

            list.add(post);
        }

        //4. 关闭连接
        conn.close();

        return list;
    }

    public List<mPost> findStudy() throws SQLException {

        Connection conn = JDBCUtil.getConnection();

        String sql = "select * from post where type = 2 ";
        PreparedStatement prep = conn.prepareStatement(sql);

        ResultSet rs = prep.executeQuery();

        List<mPost> list = new ArrayList<mPost>();
        while(rs.next()){
            //rs代表的就是每一行
            mPost post = new mPost();

            int post_id = rs.getInt("post_id");
            String post_title = rs.getString("post_title");
            String post_content = rs.getString("post_content");
            int user_id = rs.getInt("user_id");
            String type = rs.getString("type");
            int is_illegal = rs.getInt("is_illegal");
            String dates = rs.getString("dates");


            post.setPost_id(post_id);
            post.setPost_title(post_title);
            post.setPost_content(post_content);
            post.setUser_id(user_id);
            post.setType(type);
            post.setIs_illegal(is_illegal);
            post.setDates(dates);

            list.add(post);
        }

        //4. 关闭连接
        conn.close();

        return list;
    }

    //帖子被举报
    public static int illegal(String post_id) throws SQLException {
        Connection conn = JDBCUtil.getConnection();

        int post_ids = Integer.parseInt(post_id);

        String sql = "update post set is_illegal = ? where post_id = ?";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setInt(1,1);
        prep.setInt(2,post_ids);
        int row = prep.executeUpdate();
        System.out.println("删除成功"+1);

        conn.close();
        return row;
    }

    public static void main(String[] args) throws SQLException {

        mPost post = new mPost();
        //post.setPost_id(1);
        post.setPost_title("测测试");
        post.setPost_content("测试一下测试下测试一下");
        post.setUser_id(977687);
        post.setType("友");
        post.setIs_illegal(0);
        post.setDates("2022715");
        int insert = mPostDao.insert(post);
        //List<Post> list = new PostDao().findAll();
        //System.out.println(list);
        //int illegal = PostDao.illegal("13");
    }
}
